package com.machine.dao.order.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.machine.bean.OrderForm;
import com.machine.dao.order.OrderDao;

@Component("orderDao")
@Transactional
public class OrderDaoImpl implements OrderDao {
	
	private SessionFactory sessionfactory;
	
	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}

	@Resource
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}


	@Override
	public int addOrder(OrderForm orderForm) {
		this.sessionfactory.getCurrentSession().save(orderForm);
		return 0;
	}

	@Override
	public int delOrder(OrderForm orderForm) {
		
		this.sessionfactory.getCurrentSession().delete(orderForm);
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderForm> findOrder(String providerName,String buyName,int offset, int length) {
		String hql= "from OrderForm orderForm where orderForm.buyName = '" + buyName+"' OR orderForm.providerName='"+providerName+"'";
		Session session = sessionfactory.getCurrentSession();
		
		Query query = session.createQuery(hql);
		query.setFirstResult(offset);  
        query.setMaxResults(length);  
		
		List<OrderForm> list = query.list();
		
		System.out.println("list size :" + list.size());
		if(list.size() > 0){
			return list;
		}
		return null;
	}

	@Override
	public int updateOrder(OrderForm orderForm) {
		orderForm = this.findById(orderForm.getId());
		orderForm.setState(1);//设置处理完成
		
		this.sessionfactory.getCurrentSession().update(orderForm);
		return 1;
	}

	@Override
	public int deleteByIdList(List<Integer> list) {
		OrderForm orderForm = null;
		for(int id : list){
			orderForm = new OrderForm();
			orderForm.setId(id);
			this.sessionfactory.getCurrentSession().delete(orderForm);
		}
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public OrderForm findById(Integer id) {
		String hql = "from OrderForm orderForm where orderForm.id ="
			+ id + " ";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<OrderForm> orderList = query.list();
		if (orderList.size() > 0) {
			return orderList.get(0);
		}
		return null;
	}

	@Override
	public int getAllRowCount(String hql) {
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);  
		List list = query.list();  
		System.out.println("cout : " + list.size());
		return list.size();
	}

	@Override
	public List queryForPage(String providerName, String buyName, int offset,
			int length) {
		Session session = sessionfactory.getCurrentSession();
		String hql= "from OrderForm orderForm where orderForm.buyName = '" + buyName+"' OR orderForm.providerName='"+providerName+"'";
		Query query = session.createQuery(hql);  
        query.setFirstResult(offset);  
        query.setMaxResults(length);  
        List list = query.list();  
		return list;
	}


}
